/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GraphPackage;

import GUIUtilities.LabelManager;
import java.awt.Color;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseMotionAdapter;
import java.util.List;
import java.util.Vector;
import javax.swing.JLabel;

/**
 *
 * @author Heather
 */
public class BarGraph extends Graph implements GraphPolygonGrabberInterface
{

  protected final static int MIN_COLOR_CONSTRAINT = 100;
  protected final static int MAX_COLOR_CONSTRAINT = 220;
  protected double totalBarSpacing = 50;
  protected Vector<JLabel> barLabels = new Vector<JLabel>();
  protected Vector<Double> barValues = new Vector<Double>();
  protected List<List<String>> barNames;
  protected Vector<Color> currentBarColors = new Vector<Color>();
  protected Vector<Color> defaultBarColors = new Vector<Color>();
  protected Vector<Polygon> barPolygons = new Vector<Polygon>();
  protected MouseMotionAdapter toolTipListener;

  public BarGraph()
  {
    super();
    //System.out.println("MAX COLOR CONSTRAINT: " + MAX_COLOR_CONSTRAINT);
    this.addMouseListener(new java.awt.event.MouseAdapter()
    {

      public void mouseExited(java.awt.event.MouseEvent evt)
      {
        detailsLabel.setVisible(false);
      }
    });
    setupToolTipListener();
  }

  private void setupToolTipListener()
  {
    if (toolTipListener != null)
      this.removeMouseMotionListener(toolTipListener);

    toolTipListener = new java.awt.event.MouseMotionAdapter()
    {
      public void mouseMoved(java.awt.event.MouseEvent evt)
      {
        Point evtP = evt.getPoint();
        Polygon selected = null;
        boolean isInBar = false;
        int barIndex = 0;
        for (Polygon p : getBarPolygons())
        {
          if (p.contains(evtP))
          {
            isInBar = true;
            selected = p;
            break;
          }
          else
          {
            barIndex++;
          }
        }

        if (isInBar && (barNames != null))
        {
          setGraphTooltipText(barNames.get(barIndex));
          detailsLabel.setVisible(true);
          detailsLabel.setOpaque(true);
          LabelManager.resizeLabel(detailsLabel);
          detailsLabelXOffset = -detailsLabel.getSize().width;
          Point labelP = new Point(evtP.x + detailsLabelXOffset, evtP.y);
          detailsLabel.setLocation(labelP);
          setupDetailsLabel();
        }
        else
        {
          detailsLabel.setVisible(false);
        }
      }
    };
    this.addMouseMotionListener(toolTipListener);
  }

  public Vector<Polygon> getPolygons()
  {
    return barPolygons;
  }

  public Color getPolygonColor(Polygon p)
  {
    int index = barPolygons.indexOf(p);
    return currentBarColors.get(index);
  }

  public Color getColor(int i)
  {
    return currentBarColors.get(i);
  }

  public void printColorComparison(int i)
  {
    System.out.println("current: " + currentBarColors.get(i));
    System.out.println("default: " + defaultBarColors.get(i));
  }

  public void setBarNames(List<List<String>> newNames)
  {
    barNames = newNames;
    setupToolTipListener();
  }

  /**
   * @return the totalBarSpacing
   */
  public double getTotalBarSpacing()
  {
    return totalBarSpacing;
  }

  /**
   * @return the barLabels
   */
  public Vector<JLabel> getBarLabels()
  {
    return barLabels;
  }

  /**
   * @return the barValues
   */
  public Vector<Double> getBarValues()
  {
    return barValues;
  }

  /**
   * @return the currentBarColors
   */
  public Vector<Color> getCurrentBarColors()
  {
    return currentBarColors;
  }

  /**
   * @return the defaultBarColors
   */
  public Vector<Color> getDefaultBarColors()
  {
    return defaultBarColors;
  }

  /**
   * @return the barPolygons
   */
  public Vector<Polygon> getBarPolygons()
  {
    return barPolygons;
  }
}
